package kosta.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kosta.model.board.BoardVO;
import kosta.model.board.QnABoardService;
import kosta.model.member.MemberService;
import kosta.model.member.MemberVO;
import kosta.model.reply.ReplyService;
import kosta.model.reply.ReplyVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class QnABoardController extends MultiActionController {
	@Resource(name="qnABoardServiceImpl")
	private QnABoardService qnABoardService;
	@Resource(name="replyServiceImpl")
	private ReplyService replyService;
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	/**
	 * 게시판 글 목록 보기
	 * 질문게시판 전체 글 목록 보임
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/qagetListBoard.do")
	public ModelAndView getListBoard() throws SQLException{
		//System.out.println("getListBoard!!!!!!!!!");
		List<BoardVO> lvo= qnABoardService.getListBoard();
		//System.out.println(lvo + " ------------------ lvo getlist");
		return new ModelAndView ("qna_list.qnaboard","bvo",lvo);
	}
	/**
	 * 상세보기
	 * 글제목을 클릭하면 게시글에 내용이 보임
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/qafindByNo.do")
	public ModelAndView findByNo(int no) throws SQLException {
		//System.out.println("findByNo: "+no);
		//return new ModelAndView("qna_view.board","bvo",qnABoardService.findByNo(no));
		qnABoardService.findByNo(no);
		return new ModelAndView("redirect:noCountView.do?no="+no);
	}
	/**
	 * 조회수 증가 노노노
	 * 상세보기 페이지에서 새로고침시 조회수 증가 안되게
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/noCountView")
	public ModelAndView noCountView(int no) throws SQLException {
		//System.out.println("noCountView: "+no);
		BoardVO bvo=qnABoardService.noCountView(no);
		List<ReplyVO> list=replyService.getListReply(no);
		HashMap map=new HashMap();
		map.put("bvo", bvo);
		map.put("list",list);
		//System.out.println(map+" check55555");
		return new ModelAndView("qna_view.qnaboard","map",map);
	}
	/**
	 * 글쓰기 페이지로 이동
	 * @param bvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/qawrite.do")
	public String writeBoard(BoardVO bvo) throws SQLException {
		//System.out.println("------------------qawrite.do");
		return "qna_write.qnaboard";
	}
	/**
	 * 글등록, 포인트 차감
	 * 글쓰기 버튼을 클릭했을 경우 글등록에 관한 내용 보임
	 * 글이 등록 되면 50 포인트 차감 됨
	 * @param bvo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/qainsertBoard.do")
	public ModelAndView insertBoard(BoardVO bvo,HttpSession session) throws Exception{
		//System.out.println("insertBoard------------------- "+bvo);
		//50점 차감(bvo.getId())
		MemberVO vo=(MemberVO) session.getAttribute("mvo");
		System.out.println("아이디: "+bvo.getMemberVO().getId());
		qnABoardService.insertBoard(bvo);
		qnABoardService.pointMinus(bvo.getMemberVO().getId());
		session.setAttribute("mvo",memberService.login(vo));
		return new ModelAndView("redirect:qafindByNo.do?no="+bvo.getNo());
	}
	/**
	 * 업데이트 페이지로 이동
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/updatePage.do")
	public ModelAndView updatepage(int no) throws SQLException{
		//System.out.println("updatepage: "+no);
		BoardVO bvo=qnABoardService.findByNo(no);
		//System.out.println("updatepage: "+bvo);
		return new ModelAndView("qna_update.qnaboard","bvo",bvo);
	}
	/**
	 * 글수정
	 * 
	 * @param bvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/qaupdateBoard.do")
	public ModelAndView updateBoard(BoardVO bvo) throws SQLException{
		//System.out.println("updateBoard: "+bvo);
		qnABoardService.updateBoard(bvo);
		//System.out.println("cont update"+bvo.getNo());
		return new ModelAndView("redirect:qafindByNo.do?no="+bvo.getNo());
	}
	/**
	 * 글삭제
	 * 게시글 번호와 아이디를 이용하여 삭제 
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/qadeleteBoard")
	public ModelAndView deleteBoard(int no,String id) throws SQLException{
		//System.out.println("***deleteQnA****");
		qnABoardService.deleteBoard(no,id);
		return new ModelAndView("qna_list.qnaboard","bvo",qnABoardService.getListBoard());
	}
	/**
	 * 글 검색(아이디, 제목)
	 * @param value
	 * @param search
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/searchBoard.do")
	public ModelAndView searchBoard(String value, String search) throws SQLException{
		//System.out.println(value + " -------------- value");
		//System.out.println(search + " -------------- search");
		List<BoardVO> list = null;
		if(value.equals("findByTitle")){
			list=qnABoardService.findByTitle(search);
		}else if(value.equals("findById")){
			list=qnABoardService.findById(search);
		}
		//System.out.println(list + " ------------------ 검색된거");
		return new ModelAndView("qna_list.qnaboard","lvo",list);
	}
	
	
	// -----------------------댓글------------------------------
	/**
	 * 댓글 등록
	 * @param rvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/insertReply.do")
	public ModelAndView insertReply(ReplyVO rvo) throws SQLException{
		//System.out.println("*****insertReply****"+rvo);
		replyService.insertReply(rvo);
		//System.out.println("insertReply: "+rvo);
		return new ModelAndView("redirect:qafindByNo.do?no="+rvo.getBoardVO().getNo());
	}
	
	/**
	 * 댓글 삭제
	 * @param rno
	 * @param refno
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/deleteReply.do")
	public ModelAndView deleteReply(int rno, int refno,String id) throws SQLException{
		//System.out.println("******deleteReply********"+rno+" "+refno);
		replyService.deleteReply(rno, id);
		return new ModelAndView("redirect:qafindByNo.do?no="+refno);
	}
	
	/**
	 * 댓글 수정
	 * @param rvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/updateReply.do")
	public ModelAndView updateReply(ReplyVO rvo) throws SQLException{
		//System.out.println("****updateReply******"+rvo);
		replyService.updateReply(rvo);
		return new ModelAndView("redirect:qafindByNo.do?no="+rvo.getBoardVO().getNo());
	}
	
	/**
	 * 댓글에 댓글 달기
	 * @param rvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/reReply.do")
	public ModelAndView insertReReply(ReplyVO rvo) throws SQLException{
		//System.out.println("****insertReReply******"+rvo);
		replyService.insertReReply(rvo);
		return new ModelAndView("redirect:qafindByNo.do?no="+rvo.getBoardVO().getNo());
	}
	
	/**
	 * 댓글 채택
	 * @param rno
	 * @param bno
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/checkAdopt.do")
	public ModelAndView checkAdopt(String id, int no,int rno) throws SQLException {
		System.out.println("******checkAdopt******");
		System.out.println("게시글: "+no+"  댓글: "+rno);
		replyService.checkRadpot(rno); //댓글
		qnABoardService.checkAdopt(no); //게시판
		//포인트 50점 추가(id)
		System.out.println("Plus 아이디: "+id);
		replyService.pointPlus(id);
		return new ModelAndView("redirect:qafindByNo.do?no="+no);
	}
}
